<?php
namespace libraries\common;

defined('BASEPATH') OR exit('No direct script access allowed');

/**
 * Language CURD Trait
 *
 * @desc    基于多语言的增删改查, 提供相关的功能扩展
 * @version 1.0.0
 * @author  Jason
 * @date	2017年5月24日 下午8:09:45
 * @desc	该文件(Trait)的方法摘自Lib_merchant! 并加以通用(反射)处理。
 */
trait LanguageCURD
{
	/**
	 * 创建商户处理器
	 *
	 * @method  POST
	 * @author  Jason
	 * @date	2017年5月15日 下午3:18:16
	 * @param   $language
	 * @param   $name, $abstract, $summary, $latitude, $longitude, ...
	 *
	 * @return  [<status>, <content>]	一个两元素数组，第一个元素表示状态，第二个表示相应内容
	 * 	        eg:	[true, <userData>]
	 * 				[false, <errorMesg>]
	 */
	public function create($mapping = 'master')
	{
		$component = str_replace('Lib_', '', get_class());
		$component_mapping = (is_string($mapping) && $mapping !== 'master') ? "{$component}_{$mapping}" : $component;
		
		$this->CI->load->library('form_validation');
		$this->CI->load->database();
		
		$status = false;
		$content = [];
		if(FALSE === $this->CI->form_validation->run("{$component_mapping}_create")) {
			#$errorKey = explode("\n", trim(validation_errors(null, null))); // 表单辅助函数不好用
			$errorKey = $this->CI->form_validation->error_array();
			$status = false;
			$content = $errorKey;
		} else {
			$data = $this->CI->input->post();
			
			list($status, $content) = $this->{"mod_$component"}->create_instance($data, $mapping);
			if ($status) {
				// 添加语言
				$resource = $content['id'];
				$module = [$this->{"mod_$component"}, $mapping];
				$language = $data['language'];
				
				$this->lib_language->create_language($resource, $data, $module, $language);
			} else {
				// area_create_faild
				#$content = ['error'];
				$content = ['common_msg_create_faild'];
			}
		}
		
		return [$status, $content];
	}
	
	/**
	 * 更新商户处理器
	 *
	 * @method  POST
	 * @author  Jason
	 * @date	2017年5月25日 下午2:28:51
	 * @param   $id, $language
	 * @param   $name, $abstract, $summary, $latitude, $longitude, ...
	 *
	 * @return  [<status>, <content>]	一个两元素数组，第一个元素表示状态，第二个表示相应内容
	 * 	        eg:	[true, <userData>]
	 * 				[false, <errorMesg>]
	 */
	public function update($mapping = 'master')
	{
		$component = str_replace('Lib_', '', get_class());
		$component_mapping = (is_string($mapping) && $mapping !== 'master') ? "{$component}_{$mapping}" : $component;
		
		$this->CI->load->library('form_validation');
		$this->CI->load->database();
		
		$status = false;
		$content = [];
		if(FALSE === $this->CI->form_validation->run("{$component_mapping}_create")) {
			#$errorKey = explode("\n", trim(validation_errors(null, null))); // 表单辅助函数不好用
			$errorKey = $this->CI->form_validation->error_array();
			$status = false;
			$content = $errorKey;
		} else {
			$data = $this->CI->input->post();
			$id = $this->CI->input->post('id');
			unset($data['id']);
			
			list($status, $content) = $this->{"mod_$component"}->update_instance_by_id($id, $data, $mapping);
			if ($status) {
				$content = [];
				
				// 添加或刷新语言: (Flush = Replace = Delete + Insert)
				$resource = $id;
				$module = [$this->{"mod_$component"}, $mapping];
				$language = $data['language'];
				
				$this->lib_language->create_language($resource, $data, $module, $language);
			} else {
				// area_create_faild
				#$content = ['error'];
				$content = ['common_msg_update_faild'];
			}
		}
		
		return [$status, $content];
	}
	
	/**
	 * 创建商户多语言处理器
	 *
	 * @method  POST
	 * @author  Jason
	 * @date	2017年5月15日 下午3:39:12
	 * @param   $resource, $language, $name, $abstract, $summary
	 *
	 * @return  [<status>, <content>]	一个两元素数组，第一个元素表示状态，第二个表示相应内容
	 * 	        eg:	[true, <userData>]
	 * 				[false, <errorMesg>]
	 */
	public function create_language($mapping = 'master')
	{
		$component = str_replace('Lib_', '', get_class());
		$component_mapping = (is_string($mapping) && $mapping !== 'master') ? "{$component}_{$mapping}" : $component;
		
		$this->CI->load->library('form_validation');
		$this->CI->load->database();
		
		$status = false;
		$content = [];
		if(FALSE === $this->CI->form_validation->run("{$component_mapping}_create_language")) {
			#$errorKey = explode("\n", trim(validation_errors(null, null))); // 表单辅助函数不好用
			$errorKey = $this->CI->form_validation->error_array();
			$status = false;
			$content = $errorKey;
		} else {
			$data = $this->CI->input->post();
			
			// 添加或刷新语言
			$resource = $data['resource'];
			$module = [$this->{"mod_$component"}, $mapping];
			$language = $data['language'];
			
			$this->lib_language->create_language($resource, $data, $module, $language);
			
			$status = true;
		}
		
		return [$status, $content];
	}
	
	/**
	 * 删除商户处理器
	 *
	 * @method  POST
	 * @author  Jason
	 * @date	2017年7月28日 下午7:22:06
	 * @param   string $mapping 表映射，代码调用传参数(非表单参数)
	 * @param   $id
	 *
	 * @return  [<status>, <content>]	一个两元素数组，第一个元素表示状态，第二个表示相应内容
	 * 	        eg:	[true, <userData>]
	 * 				[false, <errorMesg>]
	 */
	public function delete($mapping = 'master')
	{
		$component = str_replace('Lib_', '', get_class());
		$component_mapping = (is_string($mapping) && $mapping !== 'master') ? "{$component}_{$mapping}" : $component;
		
		$this->CI->load->library('form_validation');
		$this->CI->load->database();
		
		$status = false;
		$content = [];
		if(empty($id = $this->CI->input->post_get('id'))) {
			$status = false;
			$content = ['common_msg_not_found'];
		} else {
			list($status, $content) = $this->{"mod_$component"}->delete_instance_by_id($id, $mapping);
			if ($status) {
				$status = true;
				$content = [];
				
				// MARK 这里和其它几个方法不一样！
				// 清空语言信息（删除该项对应的所有语言项目）
				$module = $component;
				$resource = $id;
				
				$this->CI->load->model('mod_language');
				$this->lib_language->delete_language($module, $resource);
			} else {
				// area_create_faild
				#$content = ['faild'];
				#$content = ['error'];
				//$content = ['common_msg_delete_faild'];
				//$content = array_merge($content, ['common_msg_delete_faild']);
				$content[] = 'common_msg_delete_faild';
			}
		}
		
		return [$status, $content];
	}
	
}